<?php

/*
*
* BEGIN player_management_ui.php
*
*
* Dazzler Player Management Uis
* @Description: The class creates all the UIs for the player management
* @Authors: Salman, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 26 September 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

class player_management_ui extends baseClass
{

    private $htmlelementsbuilder;

    private $parsesiteconfig;

    public function  __construct()
    {
    $this->htmlelementsbuilder = new htmlelementsbuilder();
    $this->parsesiteconfig = new parsesiteconfig();
    parent::parseLanguageErrorLogFile();
    }

    private function buildAddPlayerForm()
    {
        $AddPlayerWindow = null;
        $AddPlayerWindow .="<div style =' border: 1px solid transparent; padding: 7px' class='errorMessageDiv'> </div>";
        $AddPlayerWindow .="<div class='responseDiv'> </div>";
        $postPath = $this->parsesiteconfig->getURLPath();
        $AddPlayerWindow .= "<form id='addPlayerForm' method='post' name='add player form'";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('firstName', null, 'firstName', TRUE, "First Name", 'top') . "<br>";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('lastName', null, 'lastName', TRUE, "Last Name", 'top') . "<br>";

        $GameAdmin = new GameAdmin();
        $TeamArray = $GameAdmin->getAllTeamsByName();
        foreach ($TeamArray as $team) {
            $this->htmlelementsbuilder->addOption($team['TEAM_POOL_ID'], $team["TEAM_POOL_NAME"]);
        }
        $this->htmlelementsbuilder->setSelected('1');
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildDropDown("teamNameDD", "Select Player's Team", "top");
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('pob', null, 'pob', TRUE, "Place of Birth", 'top') . "<br>";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('dob', null, 'dob', TRUE, "Date of Birth", 'top') . "<br>";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('nickName', null, 'nickName', TRUE, "Nick Name", 'top') . "<br>";
         $AddPlayerWindow .=  "<div class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;margin:10px 0px 10px 0px; padding:5px 5px 5px 5px;'> ";
        $AddPlayerWindow .='<p>
	<label for="height">Player\'s Height:</label><br>
	<input type="text" id="height" style="border:0; color:#f6931f; font-weight:bold;" />
        <input type="text" id="height-feet" style="border:0; color:#f6931f; font-weight:bold;" />
        </p>
        <div id="slider-range-height"></div></div>';
          $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextArea("playerProfile", "", "playerProfile", TRUE, "Enter Player Details or Profile", "top","6","34")."<br>";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('fieldingPosition', null, 'fieldingPosition', TRUE, "Prefered Fielding Position", 'top') . "<br>";
         $this->htmlelementsbuilder->addOption("Left", "Left Handed Batsman");
       $this->htmlelementsbuilder->addOption("Right", "Right Handed Batsman");
        $this->htmlelementsbuilder->setSelected("Left");
       $AddPlayerWindow .= $this->htmlelementsbuilder->buildRadioButton("batsmanType", "Select Batsman Type", "top")."<br>";
         $AddPlayerWindow .= $this->htmlelementsbuilder->buildCheckBox("isBatsmanSelector", "Player is a Specialist Batsman")."<br>";
        $AddPlayerWindow .=  "<div class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:5px 5px 5px 5px;'> ";
       $AddPlayerWindow .= $this->htmlelementsbuilder->buildCheckBox("isBowlerSelector", "Player is a Bowler")."<br>";
       $AddPlayerWindow .= "<div id='bowlingTypeContainer'>";
        $AddPlayerWindow .= $this->htmlelementsbuilder->buildTextInput('bowlingType', null, 'bowlingType', TRUE, "Prefered Bowling Type", 'top') . "<br>";
        $AddPlayerWindow .= "</div></div>";
        $AddPlayerWindow .="</form>";
        return $AddPlayerWindow;
    }

    private function buildHREF($playerID)
    {
         $my_anchor = new htmlcodemaker('a');
        $my_anchor->set('href', '#viewPlayerDetails');
        $my_anchor->set('class', 'viewPlayerDetails');
        $my_anchor->set('name', $playerID);
        $my_anchor->set('title', 'View More Details');
        $my_anchor->set('text', 'View Details');
        return $my_anchor->output();
    }


    private function buildPlayerListTable()
    {
        $PlayerAdmin = new PlayerAdmin();
        $playerArray = $PlayerAdmin->getAllPlayers();
        
        $PlayerList = "<h2>List of All Cricket Players</h2>";

        $PlayerList .= '<table id="tablesorter-playerlist" class="tablesorter" border="0" cellpadding="0" cellspacing="1">';
        $PlayerList .= '<thead>

			<tr>
				<th>First Name</th>
				<th>Last Name</th>
				<th>Country</th>
				<th>Fielding</th>
				<th>Bowling</th>
				<th>View Details</th>
			</tr>
		</thead>';
        foreach ($playerArray as $player) {
       
       $firstName = $player['PLAYER_POOL_FNAME'];
      $LastName = $player['PLAYER_POOL_LNAME'];
      $teamName = $player['TEAM_POOL_NAME'];
      $feildingPosition = $player['PLAYER_POOL_FLDNG'];
      $bowling = $player['PLAYER_POOL_BOWLSTYL'];
      $playerID = $player['PLAYER_POOL_ID'];
      
$PlayerList .=  "<tr>";
$PlayerList .="<td>$firstName</td>";
$PlayerList .="<td>$LastName</td>";
$PlayerList .="<td>$teamName</td>";
$PlayerList .="<td>$feildingPosition</td>";
$PlayerList .="<td>$bowling</td>";
$PlayerList .="<td>".$this->buildHREF($playerID)."</td>";
	$PlayerList .=	"</tr>";
        }
                if (empty($playerArray))
                {
                  $PlayerList .=  "<tr>";
$PlayerList .="<td>Table is empty</td>";
$PlayerList .="<td>Please insert players</td>";
$PlayerList .="<td></td>";
$PlayerList .="<td></td>";
$PlayerList .="<td></td>";
$PlayerList .="<td></td>";
	$PlayerList .=	"</tr>";
                }
        $PlayerList .= "</table>";
        return $PlayerList;
    }

    private function buildDetailListItem($label,$value)
    {
        return "<span style=' display:block;margin:12px 0 0;font-weight:700;font-size:.9em;float:left;clear:left;width:300px;'><STRONG>".$label.": </STRONG></span><span style=' margin:12px 0 0;float:left;clear:right;width:90px;'> $value</span><br>";
    }
    
    private function buildViewPlayerDetails($PlayerID)
    {
       $PlayerAdmin = new PlayerAdmin();

$playerDetailsArray = $PlayerAdmin->getPlayerDetails($PlayerID);

$playerDetailsContent = "<h2>".$playerDetailsArray[0]['PLAYER_POOL_FNAME']." ".$playerDetailsArray[0]['PLAYER_POOL_LNAME']."</h2>";
$playerDetailsContent .= "<h3>".$playerDetailsArray[0]['TEAM_POOL_NAME']."</h3>";
$playerDetailsContent .= $this->buildDetailListItem("Date of Birth", $playerDetailsArray[0]['PLAYER_POOL_DOB']);
$playerDetailsContent .= $this->buildDetailListItem("Place of Birth", $playerDetailsArray[0]['PLAYER_POOL_POB']);
$playerDetailsContent .= $this->buildDetailListItem("Age", $playerDetailsArray[0]['PLAYER_POOL_AGE']);
$playerDetailsContent .= $this->buildDetailListItem("Nick Name", $playerDetailsArray[0]['PLAYER_POOL_NNAME']);
$playerDetailsContent .= $this->buildDetailListItem("Height", $playerDetailsArray[0]['PLAYER_POOL_HT']);
$playerDetailsContent .= $this->buildDetailListItem("Most Common or Prefered Fielding Position", $playerDetailsArray[0]['PLAYER_POOL_FLDNG']);
$playerDetailsContent .= $this->buildDetailListItem("Bowling Style", $playerDetailsArray[0]['PLAYER_POOL_BOWLSTYL']);
$playerDetailsContent .= $this->buildDetailListItem("Batting Style", $playerDetailsArray[0]['PLAYER_POOL_BATSTYL']);
if ($playerDetailsArray[0]['PLAYER_POOL_BATSPECILST'] == 1)
{
  $playerDetailsContent .= $this->buildDetailListItem("Specialist Batsman", 'Yes');
}
else
{
  $playerDetailsContent .= $this->buildDetailListItem("Specialist Batsman", 'No');
}
$playerDetailsContent .= "<span style=' display:block;margin:12px 0 0;font-weight:700;font-size:.9em;float:left;clear:left;width:300px;'><STRONG>Brief Profile: </STRONG></span><br><span style=' margin:12px 0 0;float:left;clear:both;'> ".$playerDetailsArray[0]['PLAYER_POOL_PROFL'];

return $playerDetailsContent;
    }






    private function buildPlayerManagementTopButtons()
    {
              $UserLogic = new UserLogic();
              $topButtons = NULL;
       if ($UserLogic->isUserCommentator())
       {
      //  $topButtons = "<span id='playerTopContainer'>";
           $topButtons = "<h2>Players</h2>";
        $topButtons .= $this->htmlelementsbuilder->buildButton("addPlayer", "Add Player", "playerManagementButtons");
        $topButtons .= $this->htmlelementsbuilder->buildButton("updatePlayerDetails", "Update Player Details", "playerManagementButtons");
        $topButtons .= $this->htmlelementsbuilder->buildButton("deletePlayers", "Remove Players", "playerManagementButtons");
    //  $topButtons = "</span>";
        }
        return $topButtons;
    }

        private function buildDeletePLayerList()
    {
        $PlayerAdmin = new PlayerAdmin();


        $GameAdmin = new GameAdmin();
        $teamsArray = $GameAdmin->getAllTeams();
//print_r($teamsArray);
        $deletePlayerContent = "<style> .deletePlayerList{float:left; 	width: 250px; border:1px solid #CCCCCC;padding:2px 2px 2px 2px; margin:2px 2px 2px 2px;}
            #selectableDeletePlayer .ui-selecting { background: #FECA40; }
	#selectableDeletePlayer .ui-selected { background: #F39814; color: white; }
	#selectableDeletePlayer { list-style-type: none; margin: 0; padding: 0; margin:10px; width: 95%; }
	#selectableDeletePlayer li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }</style>";
        $deletePlayerContent .=  "<div style =' border: 1px solid transparent; padding: 7px' class='errorMessageDiv'> </div>";
        $deletePlayerContent .= "<div id='deletePlayerListContainer'>";
        $deletePlayerContent .=    "
<p><span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 20px 0;'></span>Select the desired player to be removed and click on the delete player button to remove the player from dazzler.</p>";

        //$playersArray = $PlayerAdmin->getPlayers('21');
           // print_r($playersArray);
        foreach ($teamsArray as $teams) {
            $teamID = $teams['TEAM_POOL_ID'];
            $deletePlayerContent .= "<div class='deletePlayerList ui-corner-all'>";
            $deletePlayerContent .= "<h2>".$teams['TEAM_POOL_NAME']."</h2>";

             $playersArray = $PlayerAdmin->getPlayersByPoolID($teamID);
             $playerList = array();
             foreach ($playersArray as $player) {
              $playerItem = new htmlcodemaker('li');
              $playerItem->set(array('name'=>$player['PLAYER_POOL_ID'],'class'=>'ui-widget-content','text'=>$player['PLAYER_POOL_FNAME']." ". $player['PLAYER_POOL_LNAME']));
              $playerList[] = $playerItem;
             }

             $deleteCountryPlayerList = new htmlcodemaker('ol');
             $deleteCountryPlayerList->set(array('id'=>'selectableDeletePlayer','class'=>'selectableDeletePlayerClass'));
             $deleteCountryPlayerList->inject($playerList);
             $deletePlayerContent .= $deleteCountryPlayerList->output()."</div>";
           // $listItem

            //$deletePlayerContent .=
        }
        $deletePlayerContent .= "</div>";
      //  print_r($teamsArray);
return $deletePlayerContent;

      //  print_r($playersArray);
    }


    public function showDeletePLayerList()
    {
        return $this->buildDeletePLayerList();
    }
    public function showPlayerDetails($PlayerID)
    {
    if (isset($PlayerID))
    {
     return   $this->buildViewPlayerDetails($PlayerID);
    }

    }


    public function showPlayerTopPage()
    {
        return $this->buildPlayerManagementTopButtons();
    }

     public function showAddPlayerForm()
    {
        return $this->buildAddPlayerForm();
    }

    public function showPlayerList()
    {
        return $this->buildPlayerListTable();
    }


}